class UserController < ApplicationController
  scaffold :user
  
  layout 'model'
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @user_pages, @users = paginate :users, :per_page => 10
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = 'User was successfully created.'
#      redirect_to :action => 'list'
      send_email @user 
    else
      render :action => 'new'
    end
  end

  def send_email(user)
      email = UserMailer.deliver_register(user)
      render(:text => "<pre>" + email.encoded + "</pre>")
  end
  
  def mail
    @user = User.find(params[:id])
    email = UserMailer.deliver_register(@user)
#    render(:text => "<pre>" + email.encoded + "</pre>")
    redirect_to :action => 'list'
  end


  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = 'User was successfully updated.'
      redirect_to :action => 'show', :id => @user
    else
      render :action => 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  
end
